var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//解析图片
const bodyParser = require('body-parser')

//跨域
const cors = require('cors')

//导入配置文件
const config = require('./config/config')
//解析token的中间件
const { expressjwt: jwt } = require("express-jwt");
//路由规则
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users/users');
const myRouter = require('./routes/users/userinfo');
const cateRouter = require('./routes/articles/cate');
const addArticle = require('./routes/articles/article')
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//json接收数据最大值
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))

//在路由之前调用跨域中间件
app.use(cors())

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/my',myRouter)
app.use('/my/article',cateRouter)
app.use('/my/article',addArticle)
// app.use(jwt({secret:config.secret, algorithms: ["HS256"]}).unless({path:[/\/users/]}))



// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.send("error")
});

module.exports = app;
